using System;
using System.Windows;

namespace MvvmFx.Common.Helpers
{
    /// <summary>
    /// Static utility class containing helper methods for converters.
    /// </summary>
    public static class ConverterHelper
    {
        #region Validation Methods

        /// <summary>
        /// Is the object null or unset.
        /// </summary>
        /// <param name="value">The value to check.</param>
        /// <returns>If the value is null or unset.</returns>
        public static bool IsNullOrUnset(object value)
        {
            return ReferenceEquals(null, value) || value == DependencyProperty.UnsetValue;
        }

        /// <summary>
        /// Contains any null or unset values.
        /// </summary>
        /// <param name="values">The list of values to check.</param>
        /// <returns>If there are any null or unset values.</returns>
        public static bool ContainsNullOrUnset(object[] values)
        {
            if (ReferenceEquals(null, values))
            {
                return true;
            }

            foreach (object o in values)
            {
                if (ReferenceEquals(null, o) || o == DependencyProperty.UnsetValue)
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Is the value unset.
        /// </summary>
        /// <param name="value">The value to check.</param>
        /// <returns>If the value is unset.</returns>
        public static bool IsUnset(object value)
        {
            return value == DependencyProperty.UnsetValue;
        }

        /// <summary>
        /// If there are any unset values.
        /// </summary>
        /// <param name="values">The values to check.</param>
        /// <returns>If there are any unset values found.</returns>
        public static bool ContainsUnset(object[] values)
        {
            foreach (object o in values)
            {
                if (o == DependencyProperty.UnsetValue)
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Is there string valid.
        /// </summary>
        /// <param name="value">The string to check.</param>
        /// <returns>If the string is valid.</returns>
        public static bool IsValidString(string value)
        {
            return !(string.IsNullOrEmpty(value));
        }

        /// <summary>
        /// Is the type valid.
        /// </summary>
        /// <param name="subject">The subject to check it's type.</param>
        /// <param name="targetType">The target type used as the root of the comparision.</param>
        /// <returns>If both types are the same.</returns>
        public static bool IsValidType(object subject, Type targetType)
        {
            return subject.GetType() == targetType;
        }

        /// <summary>
        /// Is the type valid.
        /// </summary>
        /// <param name="subjects">The subjects to check their type.</param>
        /// <param name="targetType">The target type used as the root of the comparision.</param>
        /// <returns>If both types are the same.</returns>
        public static bool IsValidType(object[] subjects, Type targetType)
        {
            #region Argument Validation

            if (ReferenceEquals(subjects, null))
            {
                return false;
            }

            #endregion

            foreach (object o in subjects)
            {
                if (o.GetType() != targetType)
                {
                    return false;
                }
            }

            return true;
        }

        #endregion
    }
}